package cz.hanis.android.geography.activities;

import java.util.List;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import cz.hanis.android.geography.GeographyApplication;
import cz.hanis.android.geography.HighscoreHandler;
import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestManager;
import cz.hanis.android.geography.TestSpecification;
import cz.hanis.android.geography.models.Continent;
import cz.hanis.android.geography.models.Test;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class HighScoreTableActivity extends SherlockActivity {

	private static final int DIALOG_REMOVE_HIGHSCORE = 10;
	
	public static final int FROM_WELCOME_ACTIVITY = 0;
	public static final int FROM_RESULT_ACTIVITY = 1;
	
	public static final String FROM_ACTIVITY = "FROM_ACTIVITY";
	
	private TableLayout mLifeTable;
	private TableLayout mTimeTable;
	private HighscoreHandler mHighScoreHandler;
	
	private int from;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.highscore_table);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		from = getIntent().getIntExtra(FROM_ACTIVITY, FROM_WELCOME_ACTIVITY);
		
		mHighScoreHandler = GeographyApplication.getInstance().getHighScoreHandler();
		mLifeTable = (TableLayout) findViewById(R.id.hs_life_table);
		mTimeTable = (TableLayout) findViewById(R.id.hs_time_table);
		fillTables(mLifeTable, TestManager.TEST_TYPE_LIFE);
		fillTables(mTimeTable, TestManager.TEST_TYPE_TIME);
	}
	
	private void refreshTables() {
		mLifeTable.removeAllViews();
		fillTables(mLifeTable, TestManager.TEST_TYPE_LIFE);
		mTimeTable.removeAllViews();
		fillTables(mTimeTable, TestManager.TEST_TYPE_TIME);
	}

	private void fillTables(TableLayout table, int type) {
		TableRow row;
		TextView textView;
		List<Test> tests = GeographyApplication.getInstance().getTestManager()
				.getTests();
		int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				(float) 1, getResources().getDisplayMetrics());
		for (int i = 0; i < tests.size() + 1; i++) {
			row = new TableRow(this);
			row.setPadding(5 * dip, 5 * dip, 5 * dip, 5 * dip);
			for (int j = 0; j < Continent.getNumberOfContinents() + 1; j++) {

				textView = new TextView(this);
				// textView.setWidth(50 * dip);
				// textView.setTextColor(getResources().getColor(R.color.yellow));
				// textView.setPadding(20*dip, 0, 0, 0);
				if (i % 2 == 1) {
					textView.setTextColor(getResources().getColor(
							R.color.highscore_table_color));
					textView.setBackgroundColor(getResources().getColor(
							R.color.highscore_table_background));
				}

				if (i == 0 && j == 0) {
					textView.setText("");
				} else if (i == 0) {
					textView.setText(Continent.getNameByPosition(j - 1));
					textView.setPadding(5 * dip, 0, 0, 0);
				} else if (j == 0) {
					textView.setText(tests.get(i - 1).getName());
					textView.setPadding(0, 0, 5 * dip, 0);
				} else {
					int highscore = mHighScoreHandler.getHighScore(tests.get(i - 1).getId(), Continent.getIdByPosition(j - 1), type);
					textView.setText(String.valueOf(highscore));
					textView.setGravity(Gravity.CENTER_HORIZONTAL);
				}

				row.addView(textView);
			}

			table.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.highscore_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		
		switch (item.getItemId()) {
		case android.R.id.home:
			switch(from) {
			case FROM_RESULT_ACTIVITY:
				finish();
				return true;
			case FROM_WELCOME_ACTIVITY:
			default:
				Intent parentActivityIntent = new Intent(this,
						WelcomeActivity.class);
				parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(parentActivityIntent);
				finish();
				return true;
			}			
		case R.id.hm_delete_highscore:
			showDialog(DIALOG_REMOVE_HIGHSCORE);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	
	@Override
	protected Dialog onCreateDialog(int id) {
	  switch (id) {
	    case DIALOG_REMOVE_HIGHSCORE:
	    Builder builder = new AlertDialog.Builder(this);
	    builder.setMessage("Opravdu chcete vynulovat všechny výsledky?");
	    builder.setCancelable(true);
	    builder.setPositiveButton("Ano", new OkOnClickListener());
	    builder.setNegativeButton("Ne", new CancelOnClickListener());
	    AlertDialog dialog = builder.create();
	    dialog.show();
	   }
	  return super.onCreateDialog(id);
	}

	private final class CancelOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
		}
	}

	private final class OkOnClickListener implements
			DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			mHighScoreHandler.removeAllHighscores();
			refreshTables();
		}
	}

}
